<?php
include "antet.php";
include "func.php";

if (isset ( $_SESSION ["user"] [0], $_GET ["town"] )) {
	$_GET ["town"] = clean ( $_GET ["town"] );
	check_r ( $_GET ["town"] );
	check_u ( $_GET ["town"] );
	$town = town ( $_GET ["town"] );
	if ($town [1] != $_SESSION ["user"] [0]) {
		header ( 'Location: login.php' );
		die ();
	}
	$faction = faction ( $_SESSION ["user"] [10] );
	$r = $faction [3];
	$buildings = buildings ( $_SESSION ["user"] [10] );
	$weapons = weapons ( $_SESSION ["user"] [10] );
	$c_status = get_con ( $_GET ["town"] );
	$wq = get_w ( $_GET ["town"] );
	$uq = get_u ( $_GET ["town"] );
	$units = units ( $faction [0] );
	
	$data = explode ( "-", $town [8] );
	$res = explode ( "-", $town [10] );
	$prod = explode ( "-", $town [9] );
	$lim = explode ( "-", $town [11] );
	$out = explode ( "-", $buildings [15] [5] );
	$u_upgrades = explode ( "-", $town [17] );
	$w_upgrades = explode ( "-", $town [18] );
	$a_upgrades = explode ( "-", $town [19] );
	$s_upgrades = explode ( "-", $town [20] );
	$army = explode ( "-", $town [7] );
	$weaps = explode ( "-", $town [6] );
} else {
	header ( 'Location: login.php' );
	die ();
}
kepala($title." - ".$buildings[15][2]);
?>
<script type="text/javascript">
var res_start0=<?php echo floor($res[0]);?>;var res_start1=<?php echo floor($res[1]);?>;var res_start2=<?php echo floor($res[2]);?>;var res_start3=<?php echo floor($res[3]);?>;var res_start4=<?php echo floor($res[4]);?>;
var res_limit0=<?php echo $lim[0];?>;var res_limit1=<?php echo $lim[1];?>;var res_limit2=<?php echo $lim[1];?>;var res_limit3=<?php echo $lim[1];?>;var res_limit4=<?php echo $lim[2];?>;
var res_ph0=<?php echo ($prod[0]-$town[3]-$town[12]);?>;var res_ph1=<?php echo ($prod[1]);?>;var res_ph2=<?php echo ($prod[2]);?>;var res_ph3=<?php echo ($prod[3]);?>;var res_ph4=<?php echo ($prod[4]);?>;
var res_sec0=0;var res_sec1=0;var res_sec2=0;var res_sec3=0;var res_sec4=0;
</script>
<?php
		$to = 15;
		$output = "";
		$output .= buildingheader($to,$buildings,$data,$lim,$town);
		$cnt = "";	$li = ""; $no = ""; $na = ""; $du = ""; $ret = "";
		if (count ( $uq )) {
			//echo "Weapon forge queue:</br>";
			for($i = 0; $i < count ( $uq ); $i ++) {
				$cnt ++;
				
				$li [$i] = "cancel_u.php?town=" . $_GET ["town"] . "&type=" . $uq [$i] [1];
				$no [$i] = $uq [$i] [2];
				$na [$i] = $units [$uq [$i] [1]] [2];
				$du [$i] = $uq [$i] [0];
				$ret [$i] = "barracks.php?town=" . $_GET ["town"];
			}
		//showqueue($tasktotal, $tasklink,$taskcount,$taskitem,$taskduration,$taskupdateurl)
		$output .= showqueue ( $cnt, $li, $no, $na, $du, $ret );
		}
			$cnt = "";	$li = ""; $no = ""; $na = ""; $du = ""; $ret = "";			
		if (count ( $wq )) {
			for($i = 0; $i < count ( $wq ); $i ++) {
				$cnt ++;
				$ret [$i] = "washop.php?town=" . $_GET ["town"];
				$li [$i] = "cancel_w.php?town=" . $_GET ["town"] . "&type=" . $wq [$i] [1];
				$no [$i] = $wq [$i] [2];
				$na [$i] = $weapons [$wq [$i] [1]] [2];
				$du [$i] = $wq [$i] [0];
				//	echo "[<a class='q_link' href='cancel_w.php?town=".$_GET["town"]."&type=".$wq[$i][1]."'>x</a>] ".$wq[$i][2]." ".$weapons[$wq[$i][1]][2]." - <span id='".$i."'>".$wq[$i][0]."</span><script type='text/javascript'> var id=new Array(50); timer('".$i."', 'stable.php?town=".$_GET["town"]."'); </script></br>";
			}
		$output .= showqueue ( $cnt, $li, $no, $na, $du, $ret );
		}
		$output .= buildingstandard ( $town, $to, $lim, $buildings, $faction, $c_status, "factory.png", $lang ['expSpeed'] );
		$output .= showweaponlist ( $town, $lim, $weapons, $weaps );
		$output .= showarmylist ( $town, $units, $army, $lim );

		
		
		if ($data [15]) {
			for($i = 0; $i < count ( $units ); $i ++) {
				if ($i == 0 || $i == 1 || $i == 2 || $i == 3 || $i == 4) {
					$dur = explode ( ":", $units [$i] [9] );
					$cost = explode ( "-", $units [$i] [4] );
					$req = explode ( "-", $units [$i] [3] );
					
					$co = cost ( floor ( $cost [0] ), floor ( $cost [1] ), floor ( $cost [2] ), floor ( $cost [3] ), floor ( $cost [4] ), ($dur [0] * $lim [8] / 100) . ":" . ($dur [1] * $lim [8] / 100), "hp.png", $lang ['hp'], ($units [$i] [5] + $u_upgrades [$i]) . "</b> [" . $units [$i] [5] . "+" . $u_upgrades [$i] . "]", "atk.png", $lang ['atk'], ($units [$i] [6] + $w_upgrades [$i]) . "</b> [" . $units [$i] [6] . "+" . $w_upgrades [$i] . "]", "def.png", $lang ['def'], ($units [$i] [7] + $a_upgrades [$i]) . "</b> [" . $units [$i] [7] . "+" . $a_upgrades [$i] . "]", "spd.png", $lang ['spd'], $units [$i] [8] );
					$form = '<div class="row clearfix">
			<form role="form" name="units" method="post" action="train.php?town='.$_GET["town"].'&type='.$i.'">
				<div class="col-md-6 column"><input class="form-control" name="q" type="text" size="7" maxlength="7" value="1"></div>
				<div class="col-md-6 column"><input type="submit" class="btn btn-block btn-success" name="unit" value="'.$lang["train"].'"></div>
			</form></div>';
			
			
					$attr = unitattribute ( $units [$i], $u_upgrades [$i], $w_upgrades [$i], $a_upgrades [$i], $s_upgrades [$i] );
					
					$output .= "<br>" . boxform ( "2" . $i . ".gif", $units [$i] [2], $units [$i] [10] , $co, $form, $attr);
				}
			}
		}

		$output .= ads(1,"bottom");
		$output .= about(); 
		echo $output;
?>
